package com.zhiyume.service;

import com.zhiyume.dao.api.TUserMapper;
import com.zhiyume.dao.entity.TUser;
import com.zhiyume.dao.entity.TUserCriteria;
import com.zhiyume.dto.LoginResDTO;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.common.UserEnum;
import com.zhiyume.service.util.EncryptUtil;
import com.zhiyume.util.RedisClient;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
public class UserService {
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private RedisClient redisClient;
    public LoginResDTO Login(String userName, String password)  {
        TUserCriteria example = new TUserCriteria();
        TUserCriteria.Criteria c = example.createCriteria();
        c.andStatusEqualTo(UserEnum.USER_STATUS_NORMAL.getValue());
        c.andUserNameEqualTo(userName);
        List<TUser> lists = tUserMapper.selectByExample(example);
        LoginResDTO loginResDTO = new LoginResDTO();
        long id = -1L;
        if(null != lists && lists.size() ==1) {
            TUser tUser = lists.get(0);
            if(!tUser.getPassword().equals(EncryptUtil.SHA256(password))){
                loginResDTO.setuId(-1l);
                return loginResDTO;
            }
            id = tUser.getId();
            String token = UUID.randomUUID().toString().toLowerCase()+new Date().getTime();
            try {
                redisClient.setx(ConstantEnum.CONSTANT_TOKEN_PRE.getErrType() + token, String.valueOf(id), UserEnum.USER_TOKEN_TIME.getValue());
            }catch (Exception e){
                return loginResDTO;
            }
            loginResDTO.setToken(token);
        }
        loginResDTO.setuId(id);
        return loginResDTO;
    }
    public TUser selectUserById(Long id){
        TUserCriteria t = new TUserCriteria();
        TUserCriteria.Criteria c = t.createCriteria();
        c.andIdEqualTo(id);
        List<TUser> users = tUserMapper.selectByExample(t);
        if(CollectionUtils.isEmpty(users)){
            return null;
        }else{
            return users.get(0);
        }
    }
    public LoginResDTO Logout(Long uid,String token)  {
        TUserCriteria example = new TUserCriteria();
        TUserCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(uid);
        List<TUser> lists = tUserMapper.selectByExample(example);
        LoginResDTO loginResDTO = new LoginResDTO();
        long id = -1L;
        if(null != lists && lists.size() ==1) {
            TUser tUser = lists.get(0);
            tUser.setStatus(UserEnum.USER_STATUS_CACELED.getValue());
            tUserMapper.updateByPrimaryKey(tUser);
            try {
                redisClient.del(ConstantEnum.CONSTANT_TOKEN_PRE.getErrType() + token);
            }catch (Exception e){
                return loginResDTO;
            }
        }
        loginResDTO.setuId(id);
        return loginResDTO;
    }
}
